package org.example.kafka.consumergroup;

import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.clients.consumer.KafkaConsumer;
import org.apache.kafka.common.serialization.StringDeserializer;
import org.example.kafka.common.CommonConstant;
import org.example.kafka.common.KafkaConstants;

import java.util.Collections;
import java.util.Properties;

/**
 * 消费者群组1
 */
public class GroupAConsumer1 {
    private static KafkaConsumer<String,String> consumer = null;

    public static void main(String[] args) {
        /*消费配置的实例*/
        Properties properties
                = KafkaConstants.consumerConfig(CommonConstant.CONSUMER_GROUP_A,
                StringDeserializer.class,
                StringDeserializer.class);

        consumer = new KafkaConsumer<String, String>(properties);
        try {
            consumer.subscribe(Collections.singletonList(CommonConstant.CONSUMER_GROUP_TOPIC));
            consumer.poll(0);
            while(true){
                ConsumerRecords<String, String> records
                        = consumer.poll(500);
                for(ConsumerRecord<String, String> record:records){
                    System.out.println(String.format(
                            "主题：%s，分区：%d，偏移量：%d，key：%s，value：%s",
                            record.topic(),record.partition(),record.offset(),
                            record.key(),record.value()));
                    //do our work
                }
            }
        } finally {
            consumer.close();
        }

    }
}
